<?php

namespace app\school\controller\api;

use app\channel\ApiController;
use app\channel\model\ProxyUsersModel;
use app\school\model\TopicCourseModel;
use think\Db;

/*------------------------------------------------------ */
//-- 专题课题
/*------------------------------------------------------ */

class TrainTopic extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new TopicCourseModel();
    }


    /*------------------------------------------------------ */
    //-- 获取列表
    /*------------------------------------------------------ */
    public function getList()
    {

        $this->sqlOrder = "sort_order DESC,id DESC";
        $where = [];
        $keyword = input('keyword', '', 'trim');
        $type = input('type', 0, 'intval');
        if (empty($keyword) == false) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }
        if ($type != 0) {
            $where[] = ['type', '=', $type];
        }

        $data = $this->getPageList($this->Model, $where, 'id,add_time,view_num,virtual_num,title,title_img,description', 6);
        foreach ($data['list'] as $key => $row) {
            $row['add_time'] = dateTpl($row['add_time']);
            $row['view_num'] += $row['virtual_num'];
            unset($row['virtual_num']);
            $data['list'][$key] = $row;
        }
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 获取详情
    /*------------------------------------------------------ */
    public function getInfo()
    {
        $id = input('id', '0', 'intval');
        $is_auth = input('is_auth', '0', 'intval');
        if ($id < 1) {
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id', $id)->find();
        if (empty($row)) {
            return $this->error('没有找相关数据.');
        }
        if ($is_auth == 0) {
            $this->isAuth($is_auth);
        }
        $upData['view_num'] = ['INC', 1];
        $this->Model->where('id', $id)->update($upData);
        $row = $row->toArray();
        $row['view_num'] += $row['virtual_num'] + 1;
        $row['add_time'] = dateTpl($row['add_time']);
        $row['video_url'] = '1';
        unset($row['virtual_num']);
        $this->success($row);
    }

    /*------------------------------------------------------ */
    //--判断是否有权限查看
    /*------------------------------------------------------ */
    public function isAuth($is_auth)
    {

        $id = input('id', '0', 'intval');
        $type = input('type', '0', 'intval');
        $row = $this->Model->where('id', $id)->find();
        if ($row) {
            //类型
            $datas = explode(',', $row['user_auth']);
            $proxy_id = Db::name('channel_proxy_users')->where('user_id', $this->userInfo['user_id'])->field('proxy_id,uplevel_time')->find();
            //查询用户等级
            $data_users=Db::name('users')->where(array('user_id'=>$this->userInfo['user_id']))->field('role_id')->find();
//            $proxy_data=array(100,101,102,103,104,105);
            if (in_array($proxy_id['proxy_id'], $datas)) {
                switch ($proxy_id['proxy_id']) {
                    case 105:
                        $month = $row['retailer_time'];
                        break;
                    case 104:
                        $month = $row['wholesaler_time'];
                        break;
                    case 103:
                        $month = $row['area_time'];
                        break;
                    case 102:
                        $month = $row['city_time'];
                        break;
                    case 101:
                        $month = $row['province_time'];
                        break;
                    case 100:
                        $month = $row['country_time'];
                        break;
                    default:
                        $month = 0;
                }
            }else{
                if (in_array($data_users['role_id'], $datas)) {
                    switch ($proxy_id['proxy_id']) {
                        case 0:
                            $month = $row['common_time'];
                            break;
                        case 7:
                            $month = $row['vip_time'];
                            break;
                        default:
                            $month = 0;
                    }
                }
            }

            if ($month>=0){

                //是否有时间限制 0-表示没有时间
                if ($month != 0) {
                    $auth_time = strtotime('+' . $month . 'day', $proxy_id['uplevel_time']);
                    if ($auth_time < time()) {
                        return $this->success(array('is_status' => 0, 'msg' => "查看期限已过期"));
                    }
                }
                if ($is_auth == 0 && $type==1) {
                    return true;

                } else {
                    $row = $row->toArray();
                    $rows['video_url'] = $row['video_url'];
                    $this->success($rows);

                }

            } else {
                return $this->success(array('is_status' => 0, 'msg' => "权限不足,请先升级"));
            }

        } else {
            return $this->success(array('is_status' => 0, 'msg' => "权限不足,请先升级"));
        }


    }
    /*------------------------------------------------------ */
    //--播放视频查看是否有权限
    /*------------------------------------------------------ */
    public function authVideo()
    {
        $id = input('id', '0', 'intval');
        $is_auth = input('is_auth', '0', 'intval');
        if ($id < 1) {
            return $this->error('传参错误.');
        }
        $this->isAuth($is_auth);

    }
    /*------------------------------------------------------ */
    //--banner
    /*------------------------------------------------------ */
    public function banner()
    {
        $data_info = Db::name('school_banner')->where(array('is_show' => 1))->field('title,image,url')->order('sort_order desc,id desc')->select();
        $this->success($data_info);
    }
}